import dao.UserDao
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import QMainWindow
import dao.UserDao
import pages.LoginPage
import connector
from tools import MessageBoxTools
class Register(QMainWindow):

    def __init__(self):
        super().__init__()
        self.setupUi()
        self.show()
        self.driver = connector.driver.DatabaseDriver()

    def setupUi(self):
        self.setObjectName("mainWindow")
        self.resize(520, 410)
        font = QtGui.QFont()
        font.setPointSize(9)
        self.setFont(font)
        self.centralwidget = QtWidgets.QWidget(self)
        self.centralwidget.setObjectName("centralwidget")
        self.listView = QtWidgets.QListView(self.centralwidget)
        self.listView.setGeometry(QtCore.QRect(0, 0, 520, 400))
        self.listView.setStyleSheet("background-image: url(views/imgs/login_page_bg.png);")
        self.listView.setObjectName("listView")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(200, 50, 121, 41))
        font = QtGui.QFont()
        font.setPointSize(30)
        self.label.setFont(font)
        self.label.setStyleSheet("color: rgb(255, 0, 0);")
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(93, 145, 71, 31))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(90, 215, 75, 31))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.register_btn = QtWidgets.QPushButton(self.centralwidget)
        self.register_btn.setGeometry(QtCore.QRect(400, 195, 75, 71))
        font = QtGui.QFont()
        font.setPointSize(19)
        self.register_btn.setFont(font)
        self.register_btn.setObjectName("register_btn")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(180, 140, 180, 40))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(180, 210, 180, 40))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(25, 280, 129, 31))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.password_edit_repeat = QtWidgets.QLineEdit(self.centralwidget)
        self.password_edit_repeat.setGeometry(QtCore.QRect(180, 280, 180, 40))
        self.password_edit_repeat.setObjectName("password_edit_repeat")
        self.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(self)
        self.menubar.setGeometry(QtCore.QRect(100, 100, 800, 23))
        self.menubar.setObjectName("menubar")
        self.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(self)
        self.statusbar.setObjectName("statusbar")
        self.setStatusBar(self.statusbar)

        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

        self.register_btn.clicked.connect(self.register_btn_clicked())

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("mainWindow", "注册"))
        self.label.setText(_translate("mainWindow", "注册"))
        self.label_2.setText(_translate("mainWindow", "账号"))
        self.label_3.setText(_translate("mainWindow", "密码"))
        self.register_btn.setText(_translate("mainWindow", "登录"))
        self.label_4.setText(_translate("mainWindow", "确认密码"))

    def register_btn_clicked(self):
        uername : str = self.lineEdit.text()
        password: str = self.lineEdit_2.text()
        password_repeat: str = self.password_edit_repeat.text()
        if uername == ""or password == ""or password_repeat =="":
            MessageBoxTools.warning(title="输入错误",text="请输入用户名或密码",buttons=QMessageBox.ok)
            return
        else:
            if dao.UserDao.get_user_by_username(self.driver,user_name)is not None:
                MessageBoxTools.warning(title="提醒",text="用户名重复",buttons=QMessageBox.ok)
                return
        result: int = dao.UserDao.register_user(self.driver,user_name,password,1)
        if result >0:
            print("注册成功")
        else:
            print("注册失败")


        self.close()